<?php

include_once '../commons/Conexion.php';
include_once '../model/Categoria.php';
include_once '../model/Item.php';

/**
 * Description of CategoriaDao
 *
 * @author User
 */
class CategoriaDao {

    private $tCategoria = 't008_categoria';
    private $tItem = 't009_item';

    function __construct() {
        
    }

    /* @var $oCat Categoria 
     * @result $oCat Categoria
     */

    public function saveCategoria($oCat = null) {

        $oCat->setId(0);
        try {
            $query = 'insert into %s (nb_descripcion) values ("%s");';

            $query = sprintf($query, $this->tCategoria, $oCat->getDescripcion());

            $conn = new Conexion();
            $conn->open();
            $conn->query($query);

            $query = 'select l.co_id from %s l where l.nb_descripcion = "%s";';

            $query = sprintf($query, $this->tCategoria, $oCat->getDescripcion());

            $result = $conn->query($query);

            while ($fila = @mysql_fetch_array($result, MYSQL_ASSOC)) {
                $oCat->setId($fila['co_id']);
            }

            $conn->close();
        } catch (Exception $e) {
            
        }

        return $oCat;
    }

    /* @var $oItem Item 
     * @result $oItem Item
     */

    public function saveItem($oItem = null) {

        $oItem->setId(0);
        try {
            $query = 'insert into %s (nb_nombre,fk_categoria) values ("%s",%d);';

            $query = sprintf($query, $this->tItem, $oItem->getNombre(), $oItem->getCategoria()->getId());

            $conn = new Conexion();
            $conn->open();
            $conn->query($query);

            $query = 'select l.co_id from %s l where l.nb_nombre = "%s" and l.fk_categoria = %d;';

            $query = sprintf($query, $this->tItem, $oItem->getNombre(), $oItem->getCategoria()->getId());

            $result = $conn->query($query);

            while ($fila = @mysql_fetch_array($result, MYSQL_ASSOC)) {
                $oItem->setId($fila['co_id']);
            }

            $conn->close();
        } catch (Exception $e) {
            
        }

        return $oItem;
    }

    /*
     * @var $lId long
     * @result $oCat Categoria
     */

    public function getCatById($lId = 0) {

        $oCat = null;

        try {
            $query = 'select l.* from %s l where l.co_id = %d order by l.co_id;';
            $query = sprintf($query, $this->tCategoria, $lId);

            $conn = new Conexion();
            $conn->open();

            $result = $conn->query($query);

            if (isset($result)) {
                while ($fila = @mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $oCat = new Categoria();
                    $oCat->setId($fila['co_id']);
                    $oCat->setDescripcion($fila['nb_descripcion']);
                    $oCat->setItems($this->getItemByCat($fila['co_id']));
                }
            }

            $conn->close();
        } catch (Exception $e) {
            
        }
        return $oCat;
    }

    /*
     * @var $lId long
     * @result $oItem Item
     */

    public function getItemById($lId = 0) {

        $oItem = null;

        try {
            $query = 'select l.* from %s l where l.co_id = %d order by l.co_id;';
            $query = sprintf($query, $this->tItem, $lId);

            $conn = new Conexion();
            $conn->open();

            $result = $conn->query($query);

            if (isset($result)) {
                while ($fila = @mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $oItem = new Item();
                    $oItem->setId($fila['co_id']);
                    $oItem->setNombre($fila['nb_nombre']);
                }
            }

            $conn->close();
        } catch (Exception $e) {
            
        }
        return $oItem;
    }

    /*
     * @var $sName string
     * @result $oCat Categoria
     */

    public function getCatByName($sName = "") {

        $oCat = null;

        try {

            $query = 'select l.* from %s l where l.nb_descripcion = "%s" order by l.co_id;';
            $query = sprintf($query, $this->tCategoria, $sName);

            $conn = new Conexion();
            $conn->open();

            $result = $conn->query($query);

            if (isset($result)) {
                while ($fila = @mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $oCat = new Categoria();
                    $oCat->setId($fila['co_id']);
                    $oCat->setDescripcion($fila['nb_descripcion']);
                }
            }

            $conn->close();
        } catch (Exception $e) {
            
        }
        return $oCat;
    }

    /*
     * @var $sName string
     * @var $lId 0
     * @result $oItem Item 
     */

    public function getItemByName($sName = "", $lId = 0) {

        $oItem = null;

        try {

            $query = 'select l.* from %s l where l.nb_nombre = "%s" and l.fk_categoria = %d order by l.co_id;';
            $query = sprintf($query, $this->tItem, $sName, $lId);

            $conn = new Conexion();
            $conn->open();

            $result = $conn->query($query);

            if (isset($result)) {
                while ($fila = @mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $oItem = new Item();
                    $oItem->setId($fila['co_id']);
                    $oItem->setNombre($fila['nb_nombre']);
                }
            }

            $conn->close();
        } catch (Exception $e) {
            
        }
        return $oItem;
    }

    /*
     * 
     * @return $listCat List<Categoria>
     */

    public function getCategorias() {

        $listCat = null;

        try {
            $query = 'select l.* from %s l order by l.co_id';
            $query = sprintf($query, $this->tCategoria);

            $conn = new Conexion();
            $conn->open();

            $result = $conn->query($query);

            $listCat = array();
            $i = 0;
            if (isset($result)) {
                while ($fila = @mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $oCat = new Categoria();
                    $oCat->setId($fila['co_id']);
                    $oCat->setDescripcion($fila['nb_descripcion']);
                    $oCat->setItems($this->getItemByCat($fila['co_id']));
                    $listCat[$i++] = $oCat;
                }
            }
            $conn->close();
        } catch (Exception $e) {
            
        }

        return $listCat;
    }

    /*
     * @var $lId long
     * @return $listItem List<Item>
     */

    public function getItemByCat($lId = 0) {

        $listItem = null;

        try {
            $query = 'select l.* from %s l where l.fk_categoria = %d order by l.co_id';
            $query = sprintf($query, $this->tItem, $lId);

            $conn = new Conexion();
            $conn->open();

            $result = $conn->query($query);

            $listItem = array();
            $i = 0;
            if (isset($result)) {
                while ($fila = @mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $oItem = new Item();
                    $oItem->setId($fila['co_id']);
                    $oItem->setNombre($fila['nb_nombre']);
                    $listItem[$i++] = $oItem;
                }
            }
            $conn->close();
        } catch (Exception $e) {
            
        }
        return $listItem;
    }

    /*
     * @var $oCat Categoria
     * @result $oCat Categoria
     */

    public function updateCat($oCat) {

        try {
            $query = 'update %s l set l.nb_descripcion = "%s" where  l.co_id = %d;';
            $query = sprintf($query, $this->tCategoria, $oCat->getDescripcion(), $oCat->getId());

            $conn = new Conexion();
            $conn->open();
            $conn->query($query);
            $conn->close();
        } catch (Exception $e) {
            $oCat = null;
        }

        return $oCat;
    }

    /*
     * @var $oItem Item
     * @result $oItem Item
     */

    public function updateItem($oItem) {

        try {
            $query = 'update %s l set l.nb_nombre = "%s" where  l.co_id = %d;';
            $query = sprintf($query, $this->tItem, $oItem->getNombre(), $oItem->getId());

            $conn = new Conexion();
            $conn->open();
            $conn->query($query);
            $conn->close();
        } catch (Exception $e) {
            $oItem = null;
        }

        return $oItem;
    }

    /*
     * @var $oCat Categoria 
     * @return $result boolean
     */

    public function deleteCat($oCat) {

        $result = false;
        try {
            $query = 'delete from %s where co_id = %d';
            $query = sprintf($query, $this->tCategoria, $oCat->getId());

            $conn = new Conexion();
            $conn->open();
            $conn->query($query);
            $conn->close();

            $result = true;
        } catch (Exception $e) {
            
        }

        return $result;
    }

    /*
     * @var $oItem Item 
     * @return $result boolean 
     */

    public function deleteItem($oItem) {

        $result = false;
        try {
            $query = 'delete from %s where co_id = %d';
            $query = sprintf($query, $this->tItem, $oItem->getId());

            $conn = new Conexion();
            $conn->open();
            $conn->query($query);
            $conn->close();

            $result = true;
        } catch (Exception $e) {
            
        }

        return $result;
    }

}

?>
